package com.example.demo.controller;

import com.example.demo.model.Response;
import com.example.demo.model.entity.ReserveNumber;
import com.example.demo.model.entity.User;
import com.example.demo.service.ReserveService;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "预定接口")
public class ReserveController {
    @Resource
    private UserService userService;

    @Resource
    private ReserveService reserveService;

    @GetMapping("takeNumber")
    public Response<Integer> takeNumber(HttpServletRequest request) {
        User user = userService.userJudge(request);
        if (user == null) {
            return Response.fail(-1);
        }
        reserveService.takeNumber(user);
        return Response.success(1);
    }

    @GetMapping("deleteNumber")
    @ApiOperation(value = "取消取号", notes = "用户取消取号")
    public Response<String> deleteNumber(HttpServletRequest request) {
        User user = userService.userJudge(request);
        if (user == null) {
            return Response.fail(-1);
        }
        reserveService.deleteNumber(user);
        return Response.success("退号成功");
    }

    @GetMapping("nextNumber")
    @ApiOperation(value = "叫号", notes = "叫号，仅管理员可用")
    public Response<ReserveNumber> nextNumber(HttpServletRequest request) {
        User user = userService.userJudge(request);
        if (user == null) {
            return Response.fail(-1);
        } else if (user.getRole() != User.ADMIN) {
            return Response.fail(-5);
        }
        ReserveNumber reserveNumber = reserveService.nextNumber();
        if(reserveNumber == null){
            return Response.fail(-6);
        }
        return Response.success(reserveNumber);
    }


    @GetMapping("numberNow")
    @ApiOperation(value = "查询当前被叫号的号码")
    public Response<ReserveNumber> numberNow(HttpServletRequest request){
        User user = userService.userJudge(request);
        if (user == null) {
            return Response.fail(-1);
        }
        ReserveNumber reserveNumber = reserveService.numberNow();
        return Response.success(reserveNumber);
    }

    // TODO：等待人数
    @GetMapping("waitNumber")
    @ApiOperation(value = "等待人数", notes = "排在前面的人数")
    public Response<Integer> waitNumber(HttpServletRequest request){
        

        return Response.fail(-1);

    }
}